<template>
	<view class="con">
		<image :src="domainApi() + '/img/static/logo.png'"></image>
		<view v-if="isWeiXin">
			<button v-if="!hasWeiXinLogin"  @tap="authLogin">微信登录</button>
			<button v-else  open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">绑定手机号</button>
		</view>
<view v-else>
	<!-- 登录 -->
	<view  class="login-form">
		<view :class="['item',errorTips==1? 'error':'']">
			<view class="account">
				<text class="input-item">账号</text>
				<input type="text" @input="getInputVal" data-type="account" placeholder-class="inp-palcehoder"
					placeholder="请输入用户名"></input>
			</view>
			<view class="error-text" v-if="errorTips==1"><text class="warning-icon">!</text>请输入账号！</view>
		</view>
		<view :class="['item',errorTips==2? 'error':'']">
			<view class="account">
				<text class="input-item">密码</text>
				<input type="password" @input="getInputVal" data-type="password" placeholder-class="inp-palcehoder"
					placeholder="请输入密码"></input>
			</view>
			<view class="error-text" v-if="errorTips==2"><text class="warning-icon">!</text>请输入密码！</view>
		</view>
		<view class="operate">
			<view class="to-register" @tap="toRegitser">还没有账号？<text>去注册></text></view>
		</view>
	</view>
	
	<view>
		<button class="authorized-btn" @tap="login">登录</button>
		<button class="to-idx-btn" @tap="toIndex">回到首页</button>
	</view>
</view>
		

	</view>
</template>

<script>
	var http = require("../../utils/http");
	var util = require('../../utils/util.js');
	import {
		encrypt
	} from '../../utils/crypto.js'
	import {
		AppType
	} from '../../utils/constant.js'
	export default {
		data() {
			return {
				principal: '', // 账号
				credentials: '', // 密码
				// isPersonalCenter: false, //是否从个人中心页面跳转过来
				errorTips: 0, //错误提示
				isWeiXin: false,
				hasWeiXinLogin: false,
				
			};
		},

		components: {},
		props: {},

		computed: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			if (options.isPersonalCenter) {
				this.isPersonalCenter = options.isPersonalCenter
			}
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {
			//头部导航标题
			uni.setNavigationBarTitle({
				title: "用户登录"
			});
			if (process.env.UNI_PLATFORM === "mp-weixin") {
				console.log('微信')
				this.isWeiXin = true
				// this.authLogin()
			}
		},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},

		methods: {
			/**
			 * 输入框的值
			 */
			getInputVal: function(e) {
				const type = e.currentTarget.dataset.type;
				if (type == 'account') {
					this.setData({
						principal: e.detail.value
					});
				} else if (type == 'password') {
					this.setData({
						credentials: e.detail.value
					});
				}
			},

			/**
			 * 登录
			 */
			login() {
		

				if (this.principal.length == 0) {
					this.setData({
						errorTips: 1
					})
					return
				} else if (this.credentials.length == 0) {
					this.setData({
						errorTips: 2
					})
					return
				} else {
					this.setData({
						errorTips: 0
					})
					// #ifdef H5 || APP-PLUS
					var params = {
						url: "/login",
						method: "post",
						data: {
							"userName": this.principal,
							"passWord": encrypt(this.credentials)
						},
						callBack: res => {
							http.loginSuccess(res, () => {
								uni.showToast({
									title: "登录成功",
									icon: 'none',
									complete: () => {
										setTimeout(() => {
											wx.switchTab({
												url: '/pages/index/index'
											});
										}, 1000)

									}
								})
							})
						},
					}
					http.request(params)
					// #endif
				}
			},

			/**
			 * 去注册
			 */
			toRegitser() {
				uni.navigateTo({
					url: "/pages/register/register"
				})
			},

			/**
			 * 回到首页
			 */
			toIndex() {
				wx.switchTab({
					url: '/pages/index/index'
				});
				// this.$Router.pushTab('/pages/index/index')
			},
			onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  

					//拒绝授权后弹出一些提示  
uni.showToast({
									title: "拒绝授权",
									icon: 'none',
									});
				} else { //允许授权  
					console.log(e.detail)
					console.log(e)
					// e.detail.encryptedData      //加密的用户信息  
					// e.detail.iv     //加密算法的初始向量  时要用到  
					var params = {
						url: "/p/wx/asyncPhone",
						method: "post",
						data: {
							'jsCode': e.detail.code,
							'encryptedData': e.detail
								.encryptedData,
							'iv': e.detail.iv
						},
						callBack: res => {
							console.log(res)
								uni.showToast({
									title: "操作成功",
									icon: 'none',
									complete: () => {
										setTimeout
											(() => {
													wx.switchTab({
														url: '/pages/index/index'
													});
												},
												1000
										)
							
									}
								})
						
						},
					}
					console.log('准备请求', params)
					http.request(params)
					
				}
			},
			authLogin() {
				////弹窗授权
				//showModal用于触发uni.getUserProfile弹窗
				
						uni.getUserProfile({
							desc: '微信登录后可同步数据',
							lang: 'zh_CN',
							success: (infoRes) => {
								console.log('用户信息-----------', infoRes)
								//获取code
								uni.login({
									provider: 'weixin',
									success: (loginRes) => {
										console.log('获取code-----------', loginRes)
										var params = {
											url: "/wx/login",
											method: "post",
											data: {
												'jsCode': loginRes.code,
												'encryptedData': infoRes
													.encryptedData,
												'iv': infoRes.iv,
												'avatarUrl': infoRes.userInfo.avatarUrl,
												'nickname': infoRes.userInfo.nickName
											},
											callBack: res => {
												console.log(res)
												http.loginSuccess(res, () => {
													uni.showToast({
														title: "登录成功",
														icon: 'none',
														complete: () => {
															this.hasWeiXinLogin = true
														},
									
													})
													if(res.user.userMobile != null){
														wx.switchTab({
															url: '/pages/index/index'
														});
													}
												})
											},
										}
										console.log('准备请求', params)
										http.request(params)
									},
									fail: function(err) {
										uni.showToast({
											icon: 'none',
											title: '授权失败'
										})
									}
								});
							}
						});
				}
		},
		watch: {
			principal(nv, ov) {
				this.errorTips = 0
			}
		}
	};
</script>
<style>
	@import "./accountLogin.css";
</style>
